package com.cskaoyan.gateway.controller.dorm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.dorm.DormService;
import com.mall.dorm.constants.DormRetCode;
import com.mall.dorm.dto.IQueryDormRequest;
import com.mall.dorm.dto.IQueryDormResponse;
import com.mall.dorm.dto.IQueryResultRequest;
import com.mall.dorm.dto.IQueryResultResponse;
import com.mall.order.dto.CreateOrderRequest;
import com.mall.order.dto.CreateOrderResponse;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin(value = "*", allowCredentials = "true")
@Slf4j
@RestController
@RequestMapping("dorm")
@Api(tags = "DormController", description = "宿舍服务层")
public class DormController {

    @Reference(timeout = 30000,retries = 0,check = false)
    private DormService dormService;

    /**
     * queryDorm
     * @param request
     * @param servletRequest
     * @return
     */
    @PostMapping("query")
    public ResponseData queryDorm(@RequestBody IQueryDormRequest request, HttpServletRequest servletRequest) {

        //从TokenInterceptor设置的request域中取出当前登录的userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        //Json字符串转换为Json对象
        JSONObject object = JSON.parseObject(userInfo);
        //这里的userId是user表的id
        Integer userId = Integer.parseInt(object.get("uid").toString());

        //封装请求参数
        request.setUserId(userId);

        //远程调用
        IQueryDormResponse response = dormService.queryEmptyDorm(request);

        if (response.getCode().equals(DormRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getBuildRoomDtoList(),"success");
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * queryResult
     * @param request
     * @param servletRequest
     * @return
     */
    @PostMapping("result")
    public ResponseData queryResult(@RequestBody IQueryResultRequest request, HttpServletRequest servletRequest) {

        //从TokenInterceptor设置的request域中取出当前登录的userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        //Json字符串转换为Json对象
        JSONObject object = JSON.parseObject(userInfo);
        //这里的userId是user表的id
        Integer userId = Integer.parseInt(object.get("uid").toString());

        //封装请求参数
        request.setUserId(userId);

        //远程调用
        IQueryResultResponse response = dormService.queryDormResult(request);

        if (response.getCode().equals(DormRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
